package cac.fenjian.resource.AoTask.MsConsole.service;/*
 *@program:test-curd
 *@description:分拣台基础信息功能方法
 *@author: lsl
 *@Time: 2022/10/20  17:21
 */

import cac.fenjian.resource.AoTask.MsConsole.dao.MsConsoleDao;
import cac.fenjian.resource.AoTask.MsConsole.util.AlgorithmUtil;
import cac.fenjian.resource.PointPackageSort.service.PointPackageSortService;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class MsConsoleService {
    @Autowired
    private MsConsoleDao msConsoleDao;
    @Autowired
    private PointPackageSortService pointPackageSortService;
    /**
     * 分配工作包任务到分拣台
     * @return
     */
    @Transactional
    public String distributionTask(){
        //工作中的分拣台IP地址
        List<String> sortingTableIp = msConsoleDao.selectIp();
        //未分拣以及分拣中的工作包任务
        List<String> taskPackageId = msConsoleDao.selectMsTaskPackageId();
        //工作中的分拣台数量
        int sortingTableIpCount = sortingTableIp.size();
//        //未分拣以及分拣中的工作任务数量
//        int taskPackageCount = taskPackageId.size();
//        //平均分配数量
//        int average = taskPackageCount / sortingTableIpCount;
//        int count = 1;
        String ipAdd="";
        for (int i = 0;i<taskPackageId.size();i++){
            ipAdd=sortingTableIp.get(i%sortingTableIpCount);
            msConsoleDao.updateTaskPackage(ipAdd,taskPackageId.get(i));
        }
        return "200";
    }

    /**
     * 调用分拣任务配套的存储过程
     */
    @Transactional
    public HashMap callBle(){
        HashMap map = new HashMap();
        log.info("开始调用配套计算："+map.toString());
        msConsoleDao.callBle(map);
        //2023-08-16yebo 新增
        log.info("配套计算结束："+map.toString());
        pointPackageSortService.assginPallet();
        log.info(map.toString());
        return map;
    }

    /****
     * 每个可以使用的工作台平均分配任务
     * */
    public void algorithmTask(){
        List<String> consoles = msConsoleDao.queryAssignConsole();
        List<HashMap> tasks = msConsoleDao.queryAssignTaskPackage();
        List<List<HashMap>> list = AlgorithmUtil.aveDis(tasks,consoles.size());
        log.info("================="+list.toString());
        for(int i=0;i<consoles.size();i++){
            for(HashMap packageInfo : list.get(i)) {
                packageInfo.put("ip", consoles.get(i));
                msConsoleDao.updateTaskPackageIp(packageInfo);
            }
        }
    }
    /****
     * 下周计划针对单本工作包进行分配任务
     * */
    public void algorithmTaskByPackageId(String msTaskPackageId){
        List<String> consoles = msConsoleDao.queryMaxTaskConsoleIp();
        HashMap map = new HashMap();
        map.put("msTaskPackageId",msTaskPackageId);
        map.put("ip", consoles.get(0));
        msConsoleDao.updateTaskPackageIp(map);

    }
   //下周计划根据taskaoid向ao表插入id
    public void algorithmTaskByAoId(String msTaskAoId){
        List<String> consoles = msConsoleDao.queryMaxTaskConsoleIp();
        HashMap map = new HashMap();
        map.put("msTaskAoId",msTaskAoId);
        map.put("ip", consoles.get(0));
        msConsoleDao.updateTaskAoIp(map);

    }

    /**
     * 更新分拣台为工作状态 上线
     * @param ip
     * @return
     */
    @Transactional
    public String updateOnlineStatus(List<HashMap> list){
        String result = null;
        for(HashMap map:list){
            msConsoleDao.updateOnlineStatus(map);
        }
        return "";
    }

    /**
     * 更新分拣台为工作状态 下线
     * @param ip
     * @return
     */
    @Transactional
    public String updateOfflineStatus(List<HashMap> list){
        for(HashMap map:list){
            msConsoleDao.updateOfflineStatus(map);
        }

        return "";
    }
    //查询分拣台信息
    public QueryRespBean<HashMap> searchSortingTableByPage(QueryReqBean<HashMap> queryReqBean) {
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            Page<HashMap> dataList = msConsoleDao.selectSortingTable();
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw new DaoException(e.getMessage());
        }
    }
    //查询接收台信息
    public QueryRespBean<HashMap> selectReceiveTableByPage(QueryReqBean<HashMap> queryReqBean) {
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            Page<HashMap> dataList = msConsoleDao.selectReceiveTable();
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw new DaoException(e.getMessage());
        }
    }

    @Transactional
    public Integer insertNewConsole(HashMap map){
        Integer result = 0;
        try{
            msConsoleDao.insertNewConsole(map);
            result = 1;
        }catch (Exception e){
            e.printStackTrace();
            result = 0;
        }finally {
            return result;
        }
    }

    @Transactional
    public Integer updateMsConsole(HashMap map){
        Integer result = 0;
        try{
            msConsoleDao.updateConsole(map);
            result = 1;
        }catch (Exception e){
            e.printStackTrace();
            result = 0;
        }finally {
            return result;
        }
    }

    @Transactional
    public Integer deleteConsole(List<String> ids){
        Integer result = 0;
        try{
            for (String id:ids) {
                msConsoleDao.deleteConsole(id);
            }
            result = 1;
        }catch (Exception e){
            e.printStackTrace();
            result = 0;
        }finally {
            return result;
        }
    }

    /**
     * 手动指派自有分拣任务给其他分拣台
     * @param map
     * @return
     */
    @Transactional
    public String changeTaskToMsConsole(HashMap map){
        String result = null;
        try{
            List<String> taskPackages = (List<String>) map.get("taskPackages");
            for (String taskPackage:taskPackages
            ) {
                msConsoleDao.changeTaskPackage(taskPackage,map);
            }
            result = "200";
        }catch (Exception e){
            e.printStackTrace();
            result = "201";
        }finally {
            return result;
        }
    }

    /**
     * 查询分拣台名称
     * @return
     */
    public List<String> selectMsConsloeName(){
        List<String> msConsloeNames =  msConsoleDao.selectMsConsloeName();
        return msConsloeNames;
    }
}
